figure.Termometro = new Class({
    Extends: figure.Dial,

    initialize: function(id, css, value, unit, min, capacity, step, tempParams) {
        this.parent(id, css, value, unit, min, capacity, step);
        this.wishRange = tempParams;
        this.controlElement.change({id: id, me: this.controlElement[0], unit: unit}, this.cool);
    },

    cool: function(event) {
        var dial = $('#' + event.data.id);
        dial.text(event.data.me.value + (event.data.unit ? event.data.unit: ''));
        dial.css('width', DIAL_WIDTH + event.data.me.value.toString().length + event.data.unit.length);
    },

    addEvents: function() {
        var controlElement = this.controlElement;
        controlElement.on('cool', function cool(event, input, output, dial) {
            if (figure.stopAnimation) {
                window.setTimeout(function() { cool(event, input, output, dial) }, figure.speed);
                return;
            }
            var valueAsInt = parseInt(controlElement[0].value);
            figure.pipesStatus[output+'Start'] = figure.pipesStatus[input+'Start'];
            figure.pipesStatus[input+'End'] = figure.pipesStatus[output+'End'];
            if (valueAsInt > dial.wishRange[1] || valueAsInt < dial.wishRange[0]) {
                figure.pipesStatus[input+'End'] = false;
                figure.pipesStatus[output+'Start'] = false;
            }
            if (valueAsInt > dial.wishRange[1]) {
                valueAsInt -= 5;
                controlElement[0].value = valueAsInt;
                controlElement.trigger('change');
            }
            if (valueAsInt < dial.wishRange[0]) {
                valueAsInt += 5;
                controlElement[0].value = valueAsInt;
                controlElement.trigger('change');
            }
            if (valueAsInt <= dial.wishRange[1] && valueAsInt >= dial.wishRange[0]) {
                figure.pipesStatus[input+'End'] = figure.pipesStatus[output+'End'];
                figure.pipesStatus[output+'Start'] = figure.pipesStatus[input+'Start'];
                dial.needCool = false;
            }
            window.setTimeout(function() { cool(event, input, output, dial)}, figure.speed);
        });
    }
});

